<template>
    <a-modal :keyboard="false" :maskClosable="false" title="医院管理" :open="visible" :width="550" :confirmLoading="loading" centered @ok="handleSubmit" @cancel="handleCancel">
        <a-form layout="vertical" ref="formRef" :model="modelRef">
            <a-row :gutter="16" type="flex" justify="start">
                <a-col :xs="12" :sm="12" :md="24" v-if="modelRef.projectCode !== 'YFPHFW'">
                    <a-form-item
                        label="医院"
                        name="projectCode"
                        :rules="[
                            {
                                required: true,
                                message: '请选择医院',
                            },
                        ]"
                    >
                        <a-select v-model:value="modelRef.projectCode" placeholder="请选择医院">
                            <a-select-option v-for="item in hospitalList" :key="item.code" :value="item.code">{{ item.name }}</a-select-option>
                        </a-select>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12">
                    <a-form-item
                        label="logo"
                        name="logo"
                        :rules="[
                            {
                                required: true,
                                message: '请上传logo',
                            },
                        ]"
                    >
                        <custom-upload v-model:fileList="modelRef.logo" accept="image/*" :max-count="1" />
                        <template #extra>
                            <a-typography-text type="secondary">请参考图片上传比例为：310×280的图片;</a-typography-text>
                        </template>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12">
                    <a-form-item
                        label="banner"
                        name="banner"
                        :rules="[
                            {
                                required: true,
                                message: '请上传banner',
                            },
                        ]"
                    >
                        <custom-upload v-model:fileList="modelRef.banner" accept="image/*" :max-count="1" />
                        <template #extra>
                            <a-typography-text type="secondary">请参考图片上传比例为：630×80的传透明底图片;</a-typography-text>
                        </template>
                    </a-form-item>
                </a-col>
                <a-col :xs="12" :sm="12" :md="12">
                    <a-form-item label="状态">
                        <a-radio-group v-model:value="modelRef.status">
                            <!-- <a-radio v-for="(key, value) in statusMap" :key="value" :value="value">{{ key.text }}</a-radio> -->
                            <a-radio :value="1">启用</a-radio>
                            <a-radio :value="0">禁用</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script lang="ts">
    import type { PropType } from 'vue';
    import { defineComponent, reactive, ref, watchEffect, watch, computed } from 'vue';
    import { message } from 'ant-design-vue';
    import { cloneDeep, isUndefined } from 'lodash-es';
    import { getMedicalCaregiverHospital, createMedicalCaregiverHospital, updateMedicalCaregiverHospital } from '@/api/companion/hospital';
    import { getMedicalBuildTree } from '@/api/list/build';
    import { MinusCircleOutlined, PlusCircleOutlined } from '@ant-design/icons-vue';
    import { statusMap } from '../porter/common';

    export default defineComponent({
        props: {
            visible: {
                type: Boolean,
                required: true,
            },
            model: {
                type: Object as PropType<any | null>,
                default: () => null,
            },
        },
        emits: ['cancel', 'success'],
        setup(props, { emit }) {
            const loading = ref(false);
            const formRef = ref<any>(null);
            const hospitalList = ref<any[]>([]);
            const defaultForm = {
                status: 1,
                logo: [],
                banner: [],
            };
            const modelRef = reactive<any>(cloneDeep(defaultForm));

            // 记录初始值
            const initValues = reactive({});
            watch(
                () => props.visible,
                () => {
                    Object.assign(initValues, cloneDeep(props.model));
                },
            );
            watchEffect(async () => {
                if (props.visible) {
                    if (props.model) {
                        console.log(props.model);
                        getMedicalBuildTree().then(res => {
                            hospitalList.value = res.data.children || [];
                        });

                        if (!isUndefined(props.model.id)) {
                            getMedicalCaregiverHospital(props.model.id).then(async res => {
                                const { logo, banner, ...last } = res.data;
                                Object.assign(modelRef, last, {
                                    banner: [
                                        {
                                            url: banner,
                                            viewAddress: banner,
                                        },
                                    ],
                                    logo: [
                                        {
                                            url: logo,
                                            viewAddress: logo,
                                        },
                                    ],
                                });
                            });
                        }
                    }
                }
            });

            const handleCancel = () => {
                formRef.value.resetFields();
                emit('cancel');
                Object.keys(modelRef).forEach(key => {
                    delete modelRef[key];
                });
                Object.assign(modelRef, cloneDeep(defaultForm));
            };

            const handleSubmit = async () => {
                formRef.value.validate().then(res => {
                    loading.value = true;
                    const { logo = [], banner = [], projectCode, ...params } = modelRef;
                    if (logo.length > 0) {
                        params.logo = logo[0].viewAddress;
                    }
                    if (banner.length > 0) {
                        params.banner = banner[0].viewAddress;
                    }
                    if (projectCode) {
                        params.projectCode = projectCode;
                        params.projectName = projectCode === 'YFPHFW' ? params.projectName : hospitalList.value.find(item => item.code == projectCode)?.name;
                    }

                    (isUndefined(modelRef.id) ? createMedicalCaregiverHospital : updateMedicalCaregiverHospital)(params)
                        .then(res => {
                            if (res.code == 200) {
                                message.success('操作成功');
                                emit('success');
                                handleCancel();
                            }
                        })
                        .finally(() => {
                            loading.value = false;
                        });
                });
            };
            return {
                loading,
                formRef,
                modelRef,
                handleSubmit,
                handleCancel,
                initValues,
                statusMap,
                hospitalList,
            };
        },
        components: {
            MinusCircleOutlined,
            PlusCircleOutlined,
        },
    });
</script>
